package cn.enilu.flash.api.test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author Spark
 * @Date Created in (2019/12/06 9:35)
 * @Version v1.0
 * @Modify
 **/
public class Java8Tester {
    public static void main(String args[]){

        List<String> names1 = new ArrayList<>();
        names1.add("Google ");
        names1.add("Runoob ");
        names1.add("Taobao ");
        names1.add("Baidu ");
        names1.add("Sina ");

        List<String> names2 = new ArrayList<>();
        names2.add("Google ");
        names2.add("Runoob ");
        names2.add("Taobao ");
        names2.add("Baidu ");
        names2.add("Sina ");

        Java8Tester tester = new Java8Tester();
        System.out.println("使用 Java 7 语法: ");
        tester.sortUsingJava7(names1);
        System.out.println(names1);

        System.out.println("使用 Java 8 语法: ");
        tester.sortUsingJava8(names2);
        System.out.println(names2);
        System.out.println("--------------------------");
        List<String> strings = Arrays.asList("ef", "abc", "", "bc", "efg", "abcd","", "jkl");
        String filtered = strings.stream()
                .filter(string -> !string.isEmpty())
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.joining(","))
                ;
//        filtered.forEach(string -> System.out.println(string));
        System.out.println("筛选列表: " + filtered);
        System.out.println("--------------------------");

        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        List<Integer> squaresList = numbers.stream()
                .map( i -> i*i)
                .distinct()
//                .sorted(Comparator.comparing(x -> x))
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.toList());
//        squaresList.forEach(i -> System.out.println(i));
        System.out.println("筛选列表: " + squaresList);

        System.out.println("--------------------------");

        Random random = new Random();
        random.ints().limit(10).sorted().forEach(System.out::println);

        System.out.println("--------------------------");
        List<Integer> ints = Arrays.asList(10, 8, 4, 3, 7, 9);
        List<Integer> its = ints.stream()
                .map( x -> x * x)
                .sorted()
                .collect(Collectors.toList());
        its.forEach( x -> System.out.print(x + "\t"));
        System.out.println();
        System.out.println("--------------------------");
        IntSummaryStatistics is = ints.stream()
                .mapToInt(x -> x * x)
                .summaryStatistics();
        System.out.println("max = " + is.getMax());
        System.out.println("min = " + is.getMin());
        System.out.println("agv = " + is.getAverage());
        System.out.println("sum = " + is.getSum());
        System.out.println("cnt = " + is.getCount());

        System.out.println("--------------------------");

        List<Double> dbs = Arrays.asList(9.9, 5.3, 6.2, 8.2222);
        DoubleSummaryStatistics ds = dbs.stream()
                .mapToDouble(x -> x)
                .summaryStatistics();
        System.out.println("max = " + ds.getMax());
        System.out.println("min = " + ds.getMin());
        System.out.println("agv = " + ds.getAverage());
        System.out.println("sum = " + ds.getSum());
        System.out.println("cnt = " + ds.getCount());

    }

    // 使用 java 7 排序
    private void sortUsingJava7(List<String> names){
        Collections.sort(names, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareTo(s2);
            }
        });
    }

    // 使用 java 8 排序
    private void sortUsingJava8(List<String> names){
        Collections.sort(names, (s1, s2) -> s1.compareTo(s2));
    }
}
